package com.baizhi.controller;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import com.baizhi.annotation.LogAnnotation;
import com.baizhi.entity.Banner;
import com.baizhi.service.BannerService;
import javafx.concurrent.Worker;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/banner")
public class BannerController {
    @Autowired
    private BannerService bannerService;

    @RequestMapping("/selectAll")
    @LogAnnotation(value = "展示全部轮播图",type = "selectALL")
    public Map selectAll(int page,int limit,String bannerOldName){
        return bannerService.selectAll(page,limit,bannerOldName);
    }

    @RequestMapping("/selectOne")
    @LogAnnotation(value = "展示单个轮播图",type = "selectOne")
    public Banner selectOne(int id){
        return bannerService.selectOne(id);
    }

    @RequestMapping("/update")
    @LogAnnotation(value = "更新轮播图",type = "update")
        public Map update(Banner banner,MultipartFile file,HttpServletRequest request) throws IOException {


        if(file!=null){
            //做重命名，放置文件同名被覆盖
            String realPath = request.getServletContext().getRealPath("/img/banner");//绝对路径
            String newFilename=new Date().getTime()+"_"+file.getOriginalFilename();

            System.out.println(newFilename);
            System.out.println(banner.getBannerImageUrl());
            file.transferTo(new File(realPath+"/"+newFilename));
            banner.setBannerImageUrl("/img/banner/"+newFilename);
            return bannerService.update(banner);
        }else{
            return bannerService.updatea(banner);
        }

    }

    @RequestMapping("/insert")
    @LogAnnotation(value = "添加轮播图",type = "insert")
    public Map insert(Banner banner, MultipartFile file,HttpServletRequest request) throws IOException {
        System.out.println(file+"#######");
        System.out.println(banner+"#########");
        //做重命名，放置文件同名被覆盖
        String realPath = request.getServletContext().getRealPath("/img/banner");//绝对路径
        String newFilename=new Date().getTime()+"_"+file.getOriginalFilename();
        file.transferTo(new File(realPath+"/"+newFilename));
        banner.setBannerImageUrl("/img/banner/"+newFilename);

        System.out.println(file);
        return bannerService.insert(banner);
    }
    @RequestMapping("/delete")
    @LogAnnotation(value = "删除轮播图",type = "delete")
    public Map delete(int id){
        return bannerService.delete(id);
    }


    @RequestMapping("/updates")
    @LogAnnotation(value = "轮播图隐藏or展示",type = "updates")
    public Map updates(Banner banner){
        System.out.println(banner.getBannerId()+"bannerId");
        System.out.println(banner.getBannerState()+"bannerState");
        return bannerService.updates(banner);
    }



    //批量下载功能
    @RequestMapping("/download")
    @LogAnnotation(value = "导出轮播图数据",type = "download")
    public Map selectAlls() throws IOException {
        Map map=bannerService.selectAlls();
        return map;
    }
}
